package com.xiaocikuai.smallchunks.controller.Transaction;


import com.xiaocikuai.smallchunks.entity.Transaction.Promotional;
import com.xiaocikuai.smallchunks.entity.Transaction.PromotionalUser;
import com.xiaocikuai.smallchunks.service.Transaction.impl.PromotionalUserServiceImpl;
import com.xiaocikuai.smallchunks.utils.resultd.Resultd;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author1
 * @since 2024-07-13
 */
@RestController
@RequestMapping("/api/promotionaluser")
public class PromotionalUserController {
    @Resource
    PromotionalUserServiceImpl promotionalUserService;

    @PostMapping("/insert")
    public Resultd addPromotional(@RequestHeader("Authorization") String token, @RequestBody PromotionalUser promotionalUser) {
        if (promotionalUserService.save(promotionalUser)) {
            return Resultd.ok().data("info", "注册成功");
        } else {
            return Resultd.error().data("info", "注册失败");
        }
    }

    @PostMapping("/delete")
    public Resultd deletePromotional(@RequestHeader("Authorization") String token, @RequestBody PromotionalUser promotionalUser) {
        if (promotionalUserService.removeById(promotionalUser.getPuId())) {
            return Resultd.ok().data("info", "删除成功");
        } else {
            return Resultd.error().data("info", "删除失败");
        }
    }

    @PostMapping("/update")
    public Resultd updatePromotional(@RequestHeader("Authorization") String token, @RequestBody PromotionalUser promotionalUser) {
        if (promotionalUserService.updateById(promotionalUser)) {
            return Resultd.ok().data("info", "更新成功");
        } else {
            return Resultd.error().data("info", "更新失败");
        }
    }

    @PostMapping("/selectById")
    public Resultd selectById(@RequestHeader("Authorization") String token, @RequestBody PromotionalUser promotionalUser) {
        PromotionalUser promotional1 = promotionalUserService.getById(promotionalUser.getPuId());

        if (promotional1!= null) {
            return Resultd.ok().data("info",promotional1);
        } else {
            return Resultd.error().data("info", "未找到对应记录");
        }
    }

    //领取优惠券
    @PostMapping("/adduser")
    public Resultd adduser(@RequestHeader("Authorization") String token, @RequestBody PromotionalUser promotionalUser) {
        if (promotionalUserService.adduser(token,promotionalUser.getPromotionalId())) {
            return Resultd.ok().data("info", "领取优惠券成功");
        } else {
            return Resultd.error().data("info", "领取失败");
        }
    }
















}
